package com.cari.oa.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.SysUserEntity;
import com.cari.oa.util.StrUtils;

/**
 * 系统用户DAO类
 * User: yexuhui
 * Date: 12-12-10
 * Time: 下午4:02
 */
@Repository
public class SysUserDao extends BaseDao<SysUserEntity> {

    /**
     * 根据英文名获取系统用户实体
     * @param enname 系统用户英文名
     * @return 系统实体对象
     */
    public SysUserEntity getByEnname(String enname) {
        Query query = getSession().createQuery("from SysUserEntity where enname = :enname");
        query.setString("enname", enname);
        return (SysUserEntity)query.uniqueResult();
    }

    /**
     * 判断该英文名实体对象是否存在
     * @param enname 系统用户英文名
     * @return 是否存在
     */
    public boolean existByEnname(String enname) {
        Query query = getSession().createQuery("select count(*) from SysUserEntity where enname = ?");
        query.setParameter(0, enname);
        Long count = (Long)query.uniqueResult();
        return count > 0;
    }

    /**
     * 根据用户名和密码获取用户对象
     * @param enname 系统用户英文名
     * @param password 密码
     * @return 系统用户对象
     */
    public SysUserEntity getByNameAndPass(String enname, String password) {
        Query query = getSession().createQuery("from SysUserEntity where enname = :enname and password = :password");
        query.setString("enname", enname);
        query.setString("password", password);
        return (SysUserEntity)query.uniqueResult();
    }
    
    /**
     * 分页查询系统用户列表
     * @param userEnname 系统用户英文名
     * @param userCnname 系统用户中文名
     * @param status 系统用户状态
     * @param page 当前页
     * @param size 每页条数
     * @return 分页列表
     */
    public List<SysUserEntity> getPage(String userEnname, String userCnname, Integer status, int page, int size) {
    	List<Object> params = new ArrayList<Object>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("from SysUserEntity u where 1=1 ");

        if(StrUtils.isNotEmpty(userEnname)) {
            stringBuilder.append("and u.enname like ? ");
            params.add("%" + userEnname + "%");
        }

        if(StrUtils.isNotEmpty(userCnname)) {
            stringBuilder.append("and u.cnname like ? ");
            params.add("%" + userCnname + "%");
        }

        if(status != null) {
            stringBuilder.append("and u.status = ? ");
            params.add(status);
        }

        return getPage(stringBuilder.toString(), page, size, params.toArray());
    }
    
    /**
     * 系统用户查询记录条数
     * @param userEnname 系统用户英文名
     * @param userCnname 系统用户中文名
     * @param status 系统用户状态
     * @return 查询记录条数
     */
    public long getCount(String userEnname, String userCnname, Integer status) {
    	List<Object> params = new ArrayList<Object>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select count(*) from SysUserEntity u where 1=1 ");
        
        if(StrUtils.isNotEmpty(userEnname)) {
            stringBuilder.append("and u.enname like ? ");
            params.add("%" + userEnname + "%");
        }

        if(StrUtils.isNotEmpty(userCnname)) {
            stringBuilder.append("and u.cnname like ? ");
            params.add("%" + userCnname + "%");
        }

        if(status != null) {
            stringBuilder.append("and u.status = ? ");
            params.add(status);
        }
        
        Query query = getSession().createQuery(stringBuilder.toString());
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }
        return (Long)query.uniqueResult();
    }

	/**
	* Title: getUserAndRole
	* Description: 关联查询获取用户中文名称，英文名称，角色名称
	* @author    hjx 
	* @date       2013-1-17 
	* @return
	*/
	@SuppressWarnings("unchecked")
	public List<Map<String,String>> getUserAndRole(String id) {
		 List<Map<String,String>> list = null;
		 StringBuilder hql = new StringBuilder();		 
		 hql.append(" select new map(u.enname as enname,u.cnname as cnname,u.deptNo as deptNo");
		 if(StrUtils.isNotEmpty(id))
			hql.append(" ,spu.type as type)");
		 else
			hql.append(" )");
		 hql.append(" from SysUserEntity u");
		 if(StrUtils.isNotEmpty(id))
		    hql.append(" ,SoftProjectUserRelEntity spu");
		 hql.append(" where  u.status = 0");
		 if(StrUtils.isNotEmpty(id)){
		    hql.append(" and spu.personEnname = u.enname");
		    hql.append(" and spu.projectId='"+id+"'");
		 }
		 Query query = getSession().createQuery(hql.toString());
		 list = query.list();
		 return list;
	}
	
	/**
	 * 分页查询通讯录信息
	 * @author -- Heyyroup
	 * @2013-3-19 下午4:45:19
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysUserEntity> getContactPage(String name, String province, String deptNo, int page, int size){
		StringBuilder sb = new StringBuilder();
		sb.append("from SysUserEntity m where 1=1 ");
		
		if(StrUtils.isNotEmpty(name))
			sb.append(" and m.cnname like :usercnname ");
		
		if(StrUtils.isNotEmpty(province))
			sb.append(" and m.province like :province ");
		
		if(StrUtils.isNotEmpty(deptNo))
			sb.append(" and m.deptNo = :deptNo");
		
		sb.append(" and m.status = 0 ");
		sb.append(" order by m.deptNo, m.id ");
		
		Query query = getSession().createQuery(sb.toString());
		
		if(StrUtils.isNotEmpty(name))
			query.setParameter("usercnname", "%" + name + "%");
		
		if(StrUtils.isNotEmpty(province))
			query.setParameter("province", "%" + province + "%");
		
		if(StrUtils.isNotEmpty(deptNo))
			query.setParameter("deptNo", deptNo);
		
		query.setFirstResult((page-1)*size).setMaxResults(size);
		return query.list();
	}
	
	/**
	 * 查询符合条件的通讯录记录条数
	 * @author -- Heyyroup
	 * @2013-3-19 下午4:49:13
	 * @return
	 */
	public long getContactCount(String name, String province, String deptNo){
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from SysUserEntity m where 1=1 ");
		
		if(StrUtils.isNotEmpty(name))
			sb.append(" and m.cnname like :usercnname ");
		
		if(StrUtils.isNotEmpty(province))
			sb.append(" and m.province like :province ");
		
		if(StrUtils.isNotEmpty(deptNo))
			sb.append(" and m.deptNo = :deptNo");
		
		sb.append(" and m.status = 0 ");
		
		Query query = getSession().createQuery(sb.toString());
		
		if(StrUtils.isNotEmpty(name))
			query.setParameter("usercnname", "%" + name + "%");
		
		if(StrUtils.isNotEmpty(province))
			query.setParameter("province", "%" + province + "%");
		
		if(StrUtils.isNotEmpty(deptNo))
			query.setParameter("deptNo", deptNo);
		
		return (Long) query.uniqueResult();
	}
}
